package io.gbznt.modules.buss.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.gbznt.common.utils.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

import io.gbznt.modules.buss.dao.BussFarmlandPutIntoDao;
import io.gbznt.modules.buss.entity.BussFarmlandInfoEntity;
import io.gbznt.modules.buss.entity.BussFarmlandPutIntoEntity;
import io.gbznt.modules.buss.service.BussFarmlandInfoService;
import io.gbznt.modules.buss.service.BussFarmlandPutIntoService;
import io.gbznt.modules.sys.entity.SysDeptEntity;
import io.gbznt.modules.sys.entity.SysDictEntity;
import io.gbznt.modules.sys.service.SysDeptService;
import io.gbznt.modules.sys.service.SysDictService;


@Service("bussFarmlandPutIntoService")
public class BussFarmlandPutIntoServiceImpl extends ServiceImpl<BussFarmlandPutIntoDao, BussFarmlandPutIntoEntity> implements BussFarmlandPutIntoService {
    @Autowired
    private BussFarmlandInfoService binfo;
    @Autowired
    private SysDictService sysdict;
    @Autowired
    private SysDeptService sysdept;
	
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
    	//搜索条件--投入品---农作物----作用开始时间
    	String trp = (String) params.get("trp");
    	String nzwtj = (String) params.get("nzw");
    	String btime = (String) params.get("btime");
    	String b1 = "";
    	String b2 = "";
    	if(StringUtils.isNotBlank(btime)){
    		b1= btime.substring(0, 19);
    		b2= btime.substring(22, btime.length());
    	}

		IPage<BussFarmlandPutIntoEntity> page = this.page(
                new Query<BussFarmlandPutIntoEntity>().getPage(params),
                new QueryWrapper<BussFarmlandPutIntoEntity>()
                .like(StringUtils.isNotBlank(trp), "input_thing", trp)
                .eq(StringUtils.isNotBlank(nzwtj), "crop_dict", nzwtj)
                .between(StringUtils.isNotBlank(btime), "start_time", b1,b2)
				// 个人数据权限控制
				.in(!Constant.SUPER_ADMIN.equals(ShiroUtils.getUserId()),
						"dept_id", sysdept.getPermissionDeptIdsArr(true,true))
				.orderByDesc("create_time")
		);
        
        for (BussFarmlandPutIntoEntity bussfarm : page.getRecords()) {
			   //田块
        	  String tk = bussfarm.getFarmlandInfoId();
        	  if(StringUtils.isNotBlank(tk)){
              	BussFarmlandInfoEntity bi =  binfo.getById(tk);
              	if(bi!=null){
              		bussfarm.setFarmlandInfoIdname(bi.getName());
              	}
              	
              }
        	  //基地
        	  String jd = bussfarm.getDeptId();
        	  if(StringUtils.isNotBlank(jd)){
        		 SysDeptEntity sd =  sysdept.getById(jd);
				 if(sd!=null){
	              		bussfarm.setDeptIdname(sd.getName());
	              	}
        				  
              }
        	//农作物
        	  String nzw = bussfarm.getCropDict();
        	  if(StringUtils.isNotBlank(nzw)){
              	SysDictEntity bi =  sysdict.getById(nzw);
              	if(bi!=null){
              		bussfarm.setCropDictname(bi.getValue());
              	}
              	
              }
        	  //时间差
        	  Date starttime  = bussfarm.getStartTime();
        	  Date endtime  = bussfarm.getEndTime();
        	  Date nowdate = new Date();
        	  
        	  double samday = (double)differentDaysByMillisecond(starttime,nowdate);
        	  double sjc = (double)differentDaysByMillisecond(starttime,endtime);
        	 // float s = (float)(samday/sjc);
				if(samday!=0.0 && sjc!=0.0){
					  double s =  PrecisionUtil.div(samday, sjc)*100;
					  if(s>100){
						  s= 100;
					  }
					  bussfarm.setSjc((int)s);
				}else{
					bussfarm.setSjc(100);
				}
        	  
        	  
		}

        return new PageUtils(page);
    }
    public static double differentDaysByMillisecond(Date date1,Date date2)
    {
		double days = (double) (date2.getTime() - date1.getTime()) / (1000*3600*24);
        return days;
    }
}


